/*$(function(){*/
	//执行showTime()  
	var cache={};
	//查看订单
	 $.ajax({
			  type:"POST",
			  url:"/movie.front/orderController/getOrderByUserId",
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){   
				  if(data.result[0].statusCode!=null){
					  //跳转到登录页面
					  alert(data.result[0].statusCode);
					window.location.href="account.html";
				  }else{
					  //页面跳转到订单页面   		
					 var arr =  data.result;
					 cache.hallId = arr[0].hallId;
					for(var i=0;i<arr.length;i++){
					 var html='';
					 var seats = arr[i].orderSeats;
					      seats = seats.substring(0,seats.length-1) ;
					html = html+'<tr style="border:0px solid;"><td style="display:none;">'+arr[i].id+'</td><td style="width:200px;height:100px;border-bottom:1px solid ;border-color: #cdd9e0;text-align:center"><font size="5">'+arr[i].orderNo+'</font></td><td style="width:200px;height:100px;border-bottom:1px solid ;border-color: #cdd9e0;"><font size="5">'+arr[i].orderName+'</font></td><td style="width:200px;hieght:100px;border-bottom:1px solid;border-color: #cdd9e0;text-align:center"><font size="5">'+arr[i].orderPrice+'元</font></td><td style="width:200px;height:100px;border-bottom:1px solid ;border-color: #cdd9e0;text-align:center"><font size="5">'+arr[i].hallName+'</font></td><td style="width:200px;hieght:100px;border-bottom:1px solid;border-color: #cdd9e0;text-align:center"><font size="5">'+seats+'</font></td><td style="width:280px;hieght:100px;border-bottom:1px solid;border-color: #cdd9e0;"><font size="5">'+transfer(arr[i].orderTime)+'</font></td></tr>';
					$('#myTable-order').append(html);
					
					}
					showTime();//添加时间
				  }
			  }
			   
		   });
	   function transfer(val){
			if(val){
				val = parseInt(val, 10) * 1000;
				var date = new Date(val);
				//return date.format('yyyy-MM-dd hh:mm:ss');
				return date.format('yyyy-MM-dd hh:mm:00');
			}
			return '';
		}
		//重写javaScript日期对象原型
		Date.prototype.format = function(fmt){
			var o = {
				'M+': this.getMonth() + 1,//月份
				'd+': this.getDate(),//日
				'h+': this.getHours(),//小时
				'm+': this.getMinutes(),//分
				's+': this.getSeconds(),//秒
				'q+': Math.floor((this.getMonth() + 3) / 3),//季度
				'S': this.getMilliseconds()//毫秒
			};
			if(/(y+)/.test(fmt))
				fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
			for( var k in o)
				if(new RegExp('(' + k + ')').test(fmt))
					fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
			return fmt;
		}
	    
/*});*/

 var totalTime =15;
function showTime(){
	   totalTime -= 1;
	   var m = Math.floor(totalTime/60);
	   var s = totalTime%60;
	   $('#orderTime').empty();
	  $('#orderTime').append( '<font size="12px">请于'+m+'分'+s+'秒之内完成支付'+'</font>');
	   if(totalTime<=0){
			 //订单取消
		    var array = arrOrder();
		    var arraySeats = arrOrderSeats();
			 updateSeats(arraySeats);
		    cancelOrder(array,arraySeats);
			 //returnOrderSeats(array,arraySeats);
			 alert("对不起，您的订单未能及时支付，已经取消，请重新购买");
			
			// 
			 //页面跳转
			  window.location.href="products.html";
	   }else{
	  setTimeout("showTime()",1000);
	   //提交订单
	   
	    $('#apply-success').click(function(){
	    	 var array = arrOrder();
	    	 successOrder(array);
	    });
	  }
	   }  

function successOrder(arr){
	//查看订单
	 $.ajax({
			  type:"POST",
			  url:"/movie.front/orderController/updateSubmitOrderById",
			  data:JSON.stringify({list:arr}),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){   
				window.location.href="success.html?list="+arr;
			  }
			   
		   });
}

function cancelOrder(arr){
	//查看订单
	 $.ajax({
			  type:"POST",
			  url:"/movie.front/orderController/updateOrderById",
			  data:JSON.stringify({list:arr}),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){   
				
			  }
			   
		   });
}
function updateSeats(arr){
	var hallId = cache.hallId;
	var p = {"list":arr,"hallId":hallId}
	 $.ajax({
		  type:"POST",
		  url:"/movie.front/orderController/updateSeatsById",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){   
			
		  }
		   
	   });
}

//提交所有订单
function arrOrder(){
	 var array =new Array(); 
		for (var i = 0; i <$("#myTable-order")[0].rows.length; i++) {    //遍历Table的所有Row
		        tableInfo = $("#myTable-order")[0].rows[i].cells[0].innerText;   //获取Table中单元格的内容
		     //  alert(tableInfo);
		        array.push(tableInfo);
		}
		 array.splice(0,1);
		 
		 return array;
}

//查看座位信息
function arrOrderSeats(){
	 var array =new Array(); 
		for (var i = 1; i <$("#myTable-order")[0].rows.length; i++) {    //遍历Table的所有Row
		        tableInfo = $("#myTable-order")[0].rows[i].cells[5].innerText;   //获取Table中单元格的内容
		     //  alert(tableInfo);
		        
		        
		        array.push(tableInfo);
		}
		
		 
		 return array;
}

$(function(){
	$('#myCart').click(function(){
		$.ajax({
			  type:'POST',
			  url:'/movie.front/orderController/getOrderListsByUserId',
			  dataType:'json',
			  contentType:'application/json',
			  success:function(data){
				 if(data.result[0].statusCode!=null){
					  alert(data.result[0].statusCode);
					 window.location.href="account.html";
				 }else{
					 $('#myorders-table').empty();
					 var data =data.result;
					 for(var i=0;i<data.length;i++){
						 var html='';
						 html = html+'<tr><td  align="center">'+data[i].orderName+'</td><td  align="center">'+data[i].orderPrice+'元</td><td  align="center">'+data[i].orderTime+'</td></tr>';
						 $('#myorders-table').append(html);
					 }
				 }
			  }
		});
	});

	
	//后台程序
	var totalPage=2;//一个多少页数据
	var pageSize =6;//每页显示10条数据
	var currentPage =1;//当前第几页数据，默认为1
	var isLoading = false;//是否正在加载更多数据
	
});

